package com.cn.receive;

import com.alibaba.fastjson.JSON;
import com.cn.config.SenderConf;
import com.cn.entity.User;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by chenyuncong on 2019/5/22.
 */
@Slf4j
@Component
public class HelloReceive {


//消息的标识，false只确认当前一个消息收到，true确认所有consumer获得的消息
//channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

//ack返回false，并重新回到队列，api里面解释得很清楚
//channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);

//拒绝消息
//channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);


    //监听器监听指定的Queue
    @RabbitListener(queues="queue")
    public void processC(String str,Message message, Channel channel) {
        try {
            log.info("Receive1:"+str);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //监听器监听指定的Queue
    @RabbitListener(queues="queueUser")
    public void processA(User user,Message message, Channel channel) {    //用User作为参数
        try {
            log.info("Receive2:"+user);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //监听器监听指定的Queue
    @RabbitListener(queues="topic.message")
    public void process1(String str,Message message, Channel channel) {
        try {
            log.info("message1:"+str);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //监听器监听指定的Queue
    @RabbitListener(queues="topic.messages")
    public void process2(String str,Message message, Channel channel) {
        try {
            log.info("message2:"+str);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 正常用户队列消息监听消费者
     *
     * @param user
     * @param message
     * @param channel
     */
    @RabbitListener(queues = "${app.rabbitmq.queue.user}")
    public void userConsumer(User user, Message message, Channel channel) {
        log.info("正常用户业务监听：接收到消息:[{}]", JSON.toJSONString(user));
        try {
            //参数为：消息的DeliveryTag，是否批量拒绝，是否重新入队
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
            log.info("拒绝签收...消息的路由键为:[{}]", message.getMessageProperties().getReceivedRoutingKey());
        } catch (IOException e) {
            log.error("消息拒绝签收失败", e);
        }
    }

    /**
     * 死信队列
     * @param user
     * @param message
     * @param channel
     */
    @RabbitListener(queues = "${app.rabbitmq.queue.user-dead-letter}")
    public void userDeadLetterConsumer(User user, Message message, Channel channel) {
        log.info("接收到死信消息:[{}]", JSON.toJSONString(user));
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            log.info("死信队列签收消息....消息路由键为:[{}]", message.getMessageProperties().getReceivedRoutingKey());
        } catch (IOException e) {
            log.error("死信队列消息签收失败", e);
        }
    }



    @RabbitListener(queues = SenderConf.DELAY_PROCESS_QUEUE_NAME)
    public void getDLMessage(String msg, Message message, Channel channel){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // 模拟执行任务
            System.out.println("这是延迟队列消费：" + msg + "：" + sdf.format(new Date()));
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}